﻿var id = "";
var urls = "http://localhost/WebApi/api/";
var arr = [];
var count = 0;
var OPERATETOR = '';
var OPERATETORNAME = '';
$(function () {
    OPERATETOR = getUrlParam('usercode');
    OPERATETORNAME = getUrlParam('username');
    $("#OPERATETORNAME").val(OPERATETORNAME);
    $("#OPERATETOR").val(OPERATETOR);
    var type = getUrlParam('type');
    if (type == 'edit') {
        id = getUrlParam('id');
        GetOneInfo(id);
    }
   
})

function GetOneInfo(obj) {

    $.ajax({
        type: "Get",
        url: urls+"JobInfo/GetOneInfo",
        data: {
            id: obj,
            OPERATETOR: OPERATETOR,
            OPERATETORNAME: OPERATETORNAME
        },
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.code == "200") {
                $("#JOBSNAME").val(data.rows[0].JOBSNAME);
                $("#JOBCRON").val(data.rows[0].JOBCRON);
                $("#JOBJJ").val(data.rows[0].JOBJJ);
                $("#NGUID").val(data.rows[0].NGUID);
                arr = [];
                $("#ulFile").empty();
                count = data.data.length;
                for (var i = 0; i < data.data.length; i++) {
                    arr[i]=data.data[i].id;
                    $("#ulFile").append(' <li class="list-group-item">' + data.data[i].id + '<input class="btn btn-danger btn-xs" type="button" style="width:50px;float:right;text-align:center;" value="删除" onclick="DeleteFile(\'' + data.data[i].name + '\')" /></li>')
                }


            } else {
                parent.layer.msg(data.msg, {
                    icon: 2
                });
            }
        }
    });
}

function CheckCron(obj) {
    var sign = false;
    $.ajax({
        type: "POST",
        url: urls + "JobInfo/CheckCron",
        contentType: 'application/json',
        data: JSON.stringify({
            cron: obj
        }),
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.code == 500) {
                parent.layer.msg(data.msg, {
                    icon: 2
                });
                sign = false;
            } else if (data.code == 200) {
                sign = true;
            }
        }
    })

    return sign;
}

function getUrlParam(paras) {
    var url = window.location.href;
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof (returnValue) == "undefined") {
        return "";
    } else {
        returnValue = decodeURI(returnValue);//解决中文乱码问题
        return returnValue;
    }
}

function xzwj(obj) {

    //var postfile = $('#postfile').val();
    //if (!ChechNull(postfile)) {
    //    var postfile1 = postfile.split('\\')[postfile.split('\\').length - 1];
    //    for (var i = 0; i < arr.length; i++) {
    //        if (arr[i] == postfile1) {
    //            parent.layer.msg("附件名称不可重复！", {
    //                icon: 7
    //            });
    //            $('#postfile').val('');
    //            return;
    //        }
    //    }
    //}


    if (obj.value != "") {
        count = obj.files.length;
        for (var i = 0; i < obj.files.length; i++) {
            var size = obj.files[i].size;

            if (arr.indexOf(obj.files[i].name)>-1) {
                parent.layer.msg("文件名称不可重复！", {
                    icon: 7
                });
                $('#postfile').val('');
                return;
            }
           
            if (size > 50 * 1024 * 1024) {
                parent.layer.msg("文件不能超过50.00MB", {
                    icon: 7
                });
                obj.outerHTML = obj.outerHTML;
            }
        }
        
    }
}

function DeleteFile(obj) {
    var objs = obj;

    $.ajax({
        type: 'Get',
        url: urls + 'JobInfo/DeleteFile',
        data: {
            filePath: obj,
            OPERATETOR: OPERATETOR,
            OPERATETORNAME: OPERATETORNAME
        },
        dataType: 'json',
        async: false,
        success: function (data) {
            if (data.code == 200) {
                GetOneInfo(id);
            } else {
                parent.layer.msg(data.msg, {
                    icon: 2
                });
            }
        }
    })
}

function createrCron() {
    lyrId = layer.open({
        type: 2,
        title: 'Cron表达式',
        shadeClose: true,
        shade: 0.5,
        skin: 'layui-layer-rim',
        closeBtn: 2,
        area: ['98%', '95%'],
        shadeClose: true,
        closeBtn: 2,
        content: 'Cron.html',
        end: function (index, layero) {
            //$('#table').bootstrapTable('destroy');
            //$('#table').bootstrapTable('refresh', {
            //    pageNumber: 1
            //});
            //FWload();
        }
    });
}